<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
    <title>注册</title>
    <script src="/js/jquery-3.3.1.js"></script>

    <script>
        //在页面加载完成后
        $(function () {
            //给sex绑定blur事件
            $("#sex").blur(function () {
                //获取username文本输入框的值
                var sex = $(this).val();
                var span = $("#s_sex");
                if (sex != "男" && sex != "女") {
                    span.css("color", "red");
                    span.html("性别输入不正确");
                }
                else
                {
                    span.css("color","green");
                    span.html("正确");
                }
            });
            //给username绑定blur事件
            $("#username").blur(function () {
                //获取username文本输入框的值
                var username = $(this).val();
                //发送ajax请求
                //期望服务器响应回的数据格式：{"userExsit":true,"msg":"此用户名太受欢迎,请更换一个"}
                //                         {"userExsit":false,"msg":"用户名可用"}
                $.get("checkRegister.do",{username:username},function (data) {
                    //判断userExsit键的值是否是true

                    // alert(data);
                    var span = $("#s_username");
                    if(data.userExsit){
                        //用户名存在
                        span.css("color","red");
                        span.html(data.msg);
                    }else{
                        //用户名不存在
                        span.css("color","green");
                        span.html(data.msg);
                    }
                });

            });
        });

    </script>
</head>
<body>

<form action="/Register.do" method="post">

    <input type="text" id="username" name="username" placeholder="请输入用户名">
    <span id="s_username"></span><br>
    <input type="text" id="age" name="age" placeholder="请输入年龄"><br>
    <input type="text" id="sex" name="sex" placeholder="请输入性别">
    <span id="s_sex"></span><br>
    <input type="text" id="address" name="address" placeholder="请输入地址"><br>
    <input type="submit" value="注册"><br>

</form>
</body>
</html>
